package com.ylb.rent.controller;

import com.xiaoleilu.hutool.util.StrUtil;
import com.ylb.rent.constant.ResponseCode;
import com.ylb.rent.entity.vo.UserLoginVo;
import com.ylb.rent.exception.UserNotExistOrPasswordErrorException;
import com.ylb.rent.exception.ValidateCodeError;
import com.ylb.rent.service.IUserService;
import com.ylb.rent.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/6/28 10:58
 */
@RestController
@Api(tags = "用户相关操作",description = "用户登录、注册")
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;


    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 5001,message = "账号密码错误"),
            @ApiResponse(code = 5002,message = "验证码错误")
    })
    @ApiOperation(value = "用户登录",notes = "用户输入用户名、密码、验证码登录账号")
    @PostMapping("/login")
    public String login(@RequestBody UserLoginVo userLoginVo){
        String  token=null;
        try {
            token = userService.login(userLoginVo.getUsername(), userLoginVo.getPassword(), userLoginVo.getCode());

        } catch (UserNotExistOrPasswordErrorException e) {
            return R.errorJSONData(ResponseCode.USER_LOGIN_FAIL,"账号不存在或者密码错误");
        } catch (ValidateCodeError validateCodeError) {

            return R.errorJSONData(ResponseCode.USER_LOGIN_CODE_ERROR,"验证码错误");
        }
        System.out.println(token);
        return R.successJSONData(token);

    }
}
